﻿using System;
using System.Web.Mvc;
using DotNetNuke.UI.Modules;

namespace DnnMvcBridge.Web.Mvc.Helpers
{
	public static class DnnLinkExtensions
	{

		public static string DnnLink(this HtmlHelper helper, string actionName)
		{
			string result = "";
			var moduleContext = GetModuleContext(helper);

			if (moduleContext != null)
			{
				var urlParams = new string[1];
				if (!String.IsNullOrEmpty(actionName))
				{
					urlParams[1] = "action=" + actionName;
				}

				result = moduleContext.NavigateUrl(moduleContext.TabId, string.Empty, false, urlParams);
			}

			return result;

		}
		
		public static string DnnLink(this HtmlHelper helper, int tabId, string controlKey, string actionName)
		{
			return DnnLink(helper, tabId, controlKey, new string[]{}, actionName, 0);
		}

		public static string DnnLink(this HtmlHelper helper, int tabId, string controlKey, string[] urlParams, string actionName)
		{
			return DnnLink(helper, tabId, controlKey, urlParams, actionName, 0);
		}

		public static string DnnLink(this HtmlHelper helper, int tabId, string controlKey, string actionName, int moduleId)
		{
			return DnnLink(helper, tabId, controlKey, new string[] {}, actionName, moduleId);
		}

		public static string DnnLink(this HtmlHelper helper, int tabId, string controlKey, string[] urlParams, string actionName, int moduleId)
		{
			
			if (urlParams != null)
			{
				if (!String.IsNullOrEmpty(actionName))
				{
					Array.Resize(ref urlParams, urlParams.Length + 1);
					urlParams[urlParams.Length - 1] = "action=" + actionName;
				}
			}
			else
			{
				if (!String.IsNullOrEmpty(actionName))
				{
					urlParams = new string[] { "action=" + actionName };
				}
			}

			return DnnLink(helper, tabId, controlKey, urlParams, moduleId);


		}

		public static string DnnLink(this HtmlHelper helper, int tabId, string controlKey, int moduleId)
		{
			return DnnLink(helper, tabId, controlKey, new string[] {}, moduleId);
		}

		public static string DnnLink(this HtmlHelper helper, int tabId, string controlKey, string[] urlParams, int moduleId)
		{

			string result = "";
			
			if(moduleId > 0)
			{
					
				if (urlParams != null)
				{
					Array.Resize(ref urlParams, urlParams.Length + 1);
					urlParams[urlParams.Length - 1] = "mid=" + moduleId;
						
				}
				else
				{
					urlParams = new string[] { "mid=" + moduleId };
				}
					
				result = DnnLink(helper, tabId, controlKey, urlParams);

			}
			
			return result;

		}

		public static string DnnLink(this HtmlHelper helper, int tabId, string controlKey, string[] urlParams)
		{
			string result = "";
			var moduleContext = GetModuleContext(helper);

			if (moduleContext != null)
			{
				result = moduleContext.NavigateUrl(tabId, controlKey, false, urlParams);
			}

			return result;

		}
		
		private static ModuleInstanceContext GetModuleContext(HtmlHelper helper)
		{
			var moduleContext = helper.ViewData["ModuleContext"] as ModuleInstanceContext;
			
			if (moduleContext != null)
			{
				return moduleContext;
			}
			return null;
		}

	}
}
